package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Long count=empMapper.count();
//        Integer start=(page-1)*pageSize;
//        List<Emp> empList=empMapper.pageList(start,pageSize);
//        PageBean pageBean=new PageBean(count,empList);
//        return pageBean;
//    }
    @Override
    public PageBean page(Integer page, Integer pageSize,String name,Short gender,LocalDate begin,LocalDate end) {
        PageHelper.startPage(page,pageSize);
        List<Emp> empList=empMapper.list(name,gender,begin,end);
        Page<Emp> p= (Page<Emp>) empList;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteById(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.inster(emp);
    }

    @Override
    public List<Emp> select(Integer id) {
        List<Emp> empList=empMapper.selectList(id);
        return empList;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateList(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.selectNameAndPassword(emp);
    }

}